package mipushdemo.xiaomi.com.diyitonghang.ui.fragment.home;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.Toolbar;
import android.view.KeyEvent;
import android.view.View;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import mipushdemo.xiaomi.com.diyitonghang.R;
import mipushdemo.xiaomi.com.diyitonghang.base.BaseActivity;
import mipushdemo.xiaomi.com.diyitonghang.dao.DaoMaster;
import mipushdemo.xiaomi.com.diyitonghang.dao.DaoSession;
import mipushdemo.xiaomi.com.diyitonghang.dao.TabBeanDao;
import mipushdemo.xiaomi.com.diyitonghang.dao.TabXBeanDao;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.TabBean;
import mipushdemo.xiaomi.com.diyitonghang.data.bean.TabXBean;
import mipushdemo.xiaomi.com.diyitonghang.ui.adapter.AddTabRecy1Adapter;
import mipushdemo.xiaomi.com.diyitonghang.ui.adapter.AddTabRecyAdapter;
import mipushdemo.xiaomi.com.diyitonghang.ui.fragment.HomeFragment;

public class AddTabActivity extends BaseActivity {
    @BindView(R.id.tabToolbar)
    Toolbar tabToolbar;
    @BindView(R.id.tv_bianJi)
    TextView tvBianJi;
    @BindView(R.id.shangRecyView)
    RecyclerView shangRecyView;
    @BindView(R.id.xiaRecyView)
    RecyclerView xiaRecyView;
//    public static List<String> mXList = HomeFragment.mXTitle;
//    public static List<String> mList = HomeFragment.mXTitle;
    private AddTabRecyAdapter sAdapter;
    private AddTabRecy1Adapter xAdapter;
    private TabBeanDao tabBeanDao;
    private TabXBeanDao tabXBeanDao;
    private List<TabBean> tabBeans;
    private List<TabXBean> tabXBeans;

    @Override
    protected int windowColor() {
        return R.color.colorRed;
    }

    @Override
    protected void init() {
        changeStatusBarTextColor(true);
        tabToolbar.setTitle("");
        tabToolbar.setNavigationIcon(R.drawable.close);
        tabToolbar.setNavigationOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent();
                setResult(1,intent);

                finish();
            }
        });
        DaoSession tab = DaoMaster.newDevSession(this,"tab");
        tabBeanDao = tab.getTabBeanDao();
        tabXBeanDao = tab.getTabXBeanDao();
        tabBeans = tabBeanDao.loadAll();
        tabXBeans = tabXBeanDao.loadAll();
        shangRecyView.setLayoutManager(new GridLayoutManager(this,4));
        xiaRecyView.setLayoutManager(new GridLayoutManager(this,4));
        sAdapter = new AddTabRecyAdapter(R.layout.add_tab_item, tabBeans);
        xAdapter = new AddTabRecy1Adapter(R.layout.add_tab_item, tabXBeans);
        shangRecyView.setAdapter(sAdapter);
        xiaRecyView.setAdapter(xAdapter);
    }

    @Override
    protected void loadData() {
        xAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {

                TabBean tabBean = new TabBean(tabXBeans.get(position).getChannelId(), tabXBeans.get(position).getChannelName());
                tabBeans.add(tabBean);
                tabXBeanDao.delete(tabXBeans.get(position));
                tabBeanDao.insert(tabBean);
                tabXBeans.remove(position);
//                HomeFragment.mTitle.add(mXList.get(position));
//                HomeFragment.mFragment.add(HomeFragment.addfagment(HomeFragment.mList.get(position)));
//                HomeFragment.mXTitle.remove(position);
                xAdapter.notifyDataSetChanged();
                sAdapter.notifyDataSetChanged();
            }
        });
        sAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                //HomeFragment.addfagment(HomeFragment.mList.get(position))
                if(tabBeans.size()==1){
                    return;
                }
                TabXBean tabXBean = new TabXBean(tabBeans.get(position).getChannelId(), tabBeans.get(position).getChannelName());
                tabXBeans.add(tabXBean);
                tabBeanDao.delete(tabBeans.get(position));
                tabXBeanDao.insert(tabXBean);
                tabBeans.remove(position);
//                HomeFragment.mFragment.remove(position);
//                HomeFragment.mXTitle.add(HomeFragment.mTitle.get(position));
//                HomeFragment.mTitle.remove(position);

                sAdapter.notifyDataSetChanged();
                xAdapter.notifyDataSetChanged();
            }
        });
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_add_tab;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
            Intent intent = new Intent();
            setResult(1,intent);

            finish();
        return super.onKeyDown(keyCode, event);
    }
}
